package com.ygkj.demo

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import kotlinx.android.synthetic.main.activity_viewpage2.*
import kotlin.system.exitProcess


class Viewpage2Activity : AppCompatActivity() {
    val handler: Handler = Handler()
    lateinit var mUsbReceiver: USBReceiver
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewpage2)


        var listFrament = arrayListOf<Fragment>()

//        for (index in 0..3) {
//            var newInstance = BlankFragment.newInstance("$index", "")
//            listFrament.add(newInstance)
//        }

        banner.setAdapter(listFrament)
        banner.setonPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)

            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                var index = position + 1
                if (listFrament.size == (position + 1)) {
                    index = 0
                }

                handler.postDelayed({
                    if (index == 0) {
                        banner.getViewPage().setCurrentItem(index, true)
                    } else {
                        banner.getViewPage().currentItem = index
                    }
//(viewpage2.adapter as FragmentStateAdapter).notifyDataSetChanged()
                }, 2000)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })


//        banner.setHandlerDelay(0,1000)


//        viewpage2.setPageTransformer(DepthPageTransformer())
        viewpage2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = listFrament.size

            override fun createFragment(position: Int): Fragment = listFrament[position]
            override fun getItemId(position: Int): Long {
                return listFrament[position].hashCode().toLong()
            }

        }

//        viewpage2.adapter = FragmentPagerAdapter(supportFragmentManager,listFrament)
//        viewpage2.setfra
        viewpage2.isUserInputEnabled = false
//        viewpage2.offscreenPageLimit = 13
        viewpage2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                var index = position + 1
                if (listFrament.size == (position + 1)) {
                    index = 0


//                return
                }

                handler.postDelayed({

                    if (index == 0) {
                        viewpage2.setCurrentItem(index, false)
                    } else {
                        viewpage2.currentItem = index
                    }
//
                }, 2000)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })

        handler.postDelayed({

            for (index in 0..3) {
                var newInstance = BlankFragment.newInstance("新加的:$index", "")
                listFrament.add(newInstance)
            }
            (viewpage2.adapter as FragmentStateAdapter).notifyDataSetChanged()
//
        }, 20000)

        btn.setOnClickListener {
            val intent = intent
            overridePendingTransition(0, 0)
            finish()
            overridePendingTransition(0, 0)
            startActivity(intent)
        }

        viewpage2.setOnClickListener {

        }
//        viewpage2.
    }

    fun registerReceiver() {
        val usbDeviceStateFilter = IntentFilter()
        usbDeviceStateFilter.addAction(Intent.ACTION_MEDIA_MOUNTED)
        usbDeviceStateFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED)
        usbDeviceStateFilter.addDataScheme("file")
        mUsbReceiver = USBReceiver()
        registerReceiver(mUsbReceiver, usbDeviceStateFilter)
    }

    fun unregisterReceiver() {
        unregisterReceiver(mUsbReceiver)
    }

    override fun onDestroy() {
        super.onDestroy()
//        unregisterReceiver()
    }


    private var mExitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 判断两次点击的时间间隔（默认设置为2秒）
            if (System.currentTimeMillis() - mExitTime > 2000) {
                toast("再按一次退出程序")
                mExitTime = System.currentTimeMillis()
            } else {
                this.finish()
                exitProcess(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    fun isRunningForeground(context: Context): Boolean {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val appProcessInfos = activityManager.runningAppProcesses
        // 枚举进程
        for (appProcessInfo in appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName == context.getApplicationInfo().processName) {
                    return true
                }
            }
        }
        return false
    }

    override fun onStop() {
        super.onStop()
        var runningForeground = isRunningForeground(this)
        toast("$runningForeground")
    }


}